package com.anhry.app.safety.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.anhry.app.safety.util.bean.YearBean;

/** 
 * 2010-4-9 
 * remark: 公共方法
 * author: anhry 
 */
public class UtilMethod {
	
	private static Environment env = Environment.getInstance();
	/**
     * 根据后一个字符分割前一个字符
     * @param str String
     * @param separ String 分隔符
     * @return String[]
     */
	public static String[] separator(String str, String separ) {
		String Str[]=null;
    	if(str == null || "".equals(str)){
    		Str = new String[0];
    	}else{
    		int iStar = 0, iSum = 0;
    		for (int i = 0; i < str.length(); i++) {
    			if (str.substring(i, i + 1).equals(separ)) {
    				iSum++;
    			}
    		}
    		if (!str.substring(str.length() - 1, str.length()).equals(separ)) {
    			iSum++;
    		}
    		Str = new String[iSum];
    		iSum = 0;
    		for (int i = 0; i < str.length(); i++) {
    			if (str.substring(i, i + 1).equals(separ)) {
    				Str[iSum] = str.substring(iStar, i).trim();
    				iStar = i + 1;
    				iSum++;
    			}
    		}
    		if (!str.substring(str.length() - 1, str.length()).equals(separ)) {
    			Str[iSum] = str.substring(iStar, str.length()).trim();
    		}
    	}	
        return Str;
	}
	/**
	 * 烟花爆竹编号
	 * @param str
	 * @return
	 */
	public static String tomax(String str){
		if(str.length()==1){
			return "00"+str;
		}else if(str.length()==2){
			return "0"+str;
		}else{
			return str;
		}
	}
	/**
	 * 得到附件的大小
	 * @return
	 */
	public static String getFjSize(String size){
		String str = "0";
		DecimalFormat df2  = new DecimalFormat("###.0"); 
		if(null!=size && size.length()>0){
			int s1 = Integer.parseInt(size);
				if(s1/1024<1024){//如果小于1M
					str = df2.format(((double)s1/(double)1024))+"Kb";
					//System.out.println(df2.format(((double)s1/(double)1024))+"kb"); 
				}else if(s1/1024>=1024){//如果大于1M
					str = df2.format((s1/(1024*1024)))+"MB";
					//System.out.println(df2.format(((double)s1/(double)(1024*1024)))+"MB"); 
				}else if(s1/1024>=1024*1024){//如果大于1024MB
					str = df2.format((s1/(1024*1024*1024)))+"G";
					//System.out.println(df2.format(((double)s1/(double)(1024*1024*1024)))+"G"); 
				}
		}
		return str;
	}
	
	public static List<YearBean> getYearList(int begin){
		List<YearBean> yearList = new ArrayList<YearBean>();
		Calendar ca = Calendar.getInstance();
		int now = ca.get(Calendar.YEAR);
		for(int i=begin;i<(now+1);i++){
			YearBean y = new YearBean();
			y.setId(String.valueOf(i));
			y.setYearName(String.valueOf(i));
			yearList.add(y);
		}
		return yearList;
	}
	
	public static int getNowDate(Date date,int type){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH)+1;
		int quarter = 0;
		if(ca != null){
			int month1 = ca.get(Calendar.MONTH);
			if (month1 < 3) {
				quarter = 1;
			} else if (month1 < 6) {
				quarter = 2;
			} else if (month1 < 9) {
				quarter = 3;
			} else {
				quarter = 4;
			}
		}
		if(type==0){//年
			return year;
		}else if(type==1){//季度
			return quarter;
		}else if(type == 3){//月
			return month;
		}
		return 0;
	}
	
	/**
	 * 将0900.00格式转换为900.00
	 * @param str
	 * @return
	 */
	public static String getNoZeroStr(String str){
		if(null!=str && str.length()>0){
			if(str.indexOf(".")==-1){
				return String.valueOf(Integer.parseInt(str));
			}else{
				int i = str.indexOf(".");
				return String.valueOf(Integer.parseInt(str.substring(0,str.length()-3)))+str.substring(i, str.length());
			}
		}else{
			return "0";
		}
	}
	
	public static boolean urlFilter(String url){
		boolean b = false;
		try {
			if(null!=url && url.length()>0){
				if(url.contains("#")){
					return true;
				}
				if(url.contains("<script>")){
					return true;
				}
				if(url.contains("alert")){
					return true;
				}
				if(url.contains("*")){
					return true;
				}
				if(url.contains("'")){
					return true;
				}
				if(url.contains("\"")){
					return true;
				}
				if(url.contains(")")){
					return true;
				}
				if(url.contains(";")){
					return true;
				}
				if(url.contains("<>")){
					return true;
				}
				if(url.contains("<")){
					return true;
				}
				if(url.contains(">")){
					return true;
				}
				if(url.contains("+")){
					return true;
				}
				if(url.contains("(")){
					return true;
				}
				if(url.contains("()")){
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
	/**
	 * 判断图片格式
	 * @param types
	 * @return
	 */
	public static boolean filterType(String conType,String[] types){
		boolean re = false;
		for (String type : types){
			if (type.equalsIgnoreCase(conType)){
				re = true;
				break;
			}else{
				re = false;
			}
		}
		return re;
	}
	
	/**
	 * 得到自查自报查看年
	 * @return
	 */
	public static String getZclYear(){
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DATE);
		if(day<15){//如果当前日小于8号
			if(month==1){//如果是一月
				return year-1+"";
			}else{
				return year+"";
			}
		}else{
				return year+"";
		}
	}
	
	/**
	 * 得到自查自报查看月
	 * @return
	 */
	public static String getZclMonth(){
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DATE);
		if(day<env.getIntProperty("corp.half.month.middle")){//如果当前日小于15号
			if(month==1){//如果是一月上半月，则显示上一年的12月份
				return "12";
			}else{
				return month-1+"";
			}
		}else{
				return month+"";
		}
	}
	
	/**
	 * 得到自查年
	 * @return
	 */
	public static String getZcYear(){
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		//int day = cal.get(Calendar.DATE);
		if(month==1 && day < env.getIntProperty("corp.half.month.middle")){//如果是一月并且是15号之前，则显示上一年
			return year-1+"";
		}else{
			return year+"";
		}
	}
	
	/**
	 * 得到自查月
	 * @return
	 */
	public static String getZcMonth(){
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DATE);
		if(day<env.getIntProperty("corp.half.month.start")){//如果当前日小于8号
			if(month==1){//如果是一月
				return "12";
			}else{
				return month-1+"";
			}
		}else{
			return month+"";
		}
	}
	
	public static String getZcBdate(int year,int month){
		return year+"-"+month+"-1";
	}
	
	public static String getZcEdate(int year,int month){
			int day = 30;
			if(month == 2) {
				if(year %4 == 0) {
					day = 29;
				} else {
					day = 28;
				}
			}
			if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12 ) {
				day = 31;
			}
			return year+"-"+month+ "-" + day;
		
	}
	
	/**
	 * 验证数字
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str){
        java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("[0-9]*");
        java.util.regex.Matcher match=pattern.matcher(str);
        if(match.matches()==false){
           return false;
        }
        else{
           return true;
        }
    }
	
	/**
	 * 验证日期格式
	 * @param sDate
	 * @return
	 */
	public static boolean isValidDate(String sDate) {
		String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
		String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
				+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
				+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
				+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
				+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
				+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		if ((sDate != null)) {
			Pattern pattern = Pattern.compile(datePattern1);
			Matcher match = pattern.matcher(sDate);
			if (match.matches()) {
				pattern = Pattern.compile(datePattern2);
				match = pattern.matcher(sDate);
				return match.matches();
			} else {
				return false;
			}
		}
		return false;
	}
	
	
	public static ApplicationContext getSpringContext(){
	    ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext();
	    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
	    return ctx;
	}
	
	public static void main(String[] args){
		
		
		System.out.println(UtilMethod.getZclYear());
	}
}
